/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package evotingclient.data;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Agnieszka
 */
@XmlRootElement()
public class VotingData {

    private Integer sId = 10;
    private String name;
    private List<Card> cards = new LinkedList<Card>();
    private Card skipCard;
    private int step = 0;
    //po usunieciu skip

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public void prepareR(List<Card> c) throws Exception {
        if (skipCard == null) {
            throw new Exception("Skip should be not null!!!");
        }
        for (int i = 0; i < cards.size(); ++i) {
            cards.get(i).setR(c.get(i).getR());
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Card> getCards() {
        return cards;
    }

    public void setCards(List<Card> cards) {
        this.cards = cards;
    }

    public Integer getsId() {
        return sId;
    }

    public void setsId(Integer sId) {
        this.sId = sId;
    }

    public Card getSkipCard() {
        return skipCard;
    }

    public void setSkipCard(Card skipCard) {
        this.skipCard = skipCard;
    }

    /**
     * 
     * 
     * @param card - template card
     * @param number - number of generated card
     */
    public void generate(SecureRandom random, Card card, Integer number) throws UnsupportedEncodingException {
        for (int i = 0; i < number; i++) {
            Card c = card.clone();
            c.setNumber(i + 1);
            BigInteger serial = Tool.nextSerialNumber(random);
            for (Vote v : c.getVotes()) {
                v.setVoteCard(serial);
                v.calculateHashVote();
            }
            getCards().add(c);
        }
    }

    public void writeToFile(Card card, String path) {
        try {
            //use buffering
            OutputStream file = new FileOutputStream(path);
            OutputStream buffer = new BufferedOutputStream(file);
            ObjectOutput output = new ObjectOutputStream(buffer);
            try {
                output.writeObject(card);
            } finally {
                output.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(Card.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Card readFromFile(String path) {
        try {
            //use buffering
            InputStream file = new FileInputStream(path);
            InputStream buffer = new BufferedInputStream(file);
            ObjectInput input = new ObjectInputStream(buffer);
            try {
                //deserialize the List
                Card card = (Card) input.readObject();
                return card;
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(VotingData.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(Card.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
